package cn.topevery.garden.dal.logic;

import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjRequestFundsPlanDao;
import cn.topevery.garden.entity.dto.ProjMoneyDTO;
import cn.topevery.garden.entity.dto.ProjRequestFundsPlanFilesDTO;
import cn.topevery.garden.entity.po.ProjBid;
import cn.topevery.garden.entity.po.ProjFiles;
import cn.topevery.garden.entity.po.ProjRequestFundsPlan;
import cn.topevery.garden.entity.query.ProjRequestFundsPlanQuery;
import cn.topevery.garden.entity.vo.ProjFilesVO;
import cn.topevery.garden.entity.vo.ProjPaymentMoneyVO;
import cn.topevery.garden.entity.vo.ProjRequestFundsPlanVO;
import cn.topevery.garden.framework.base.BaseLogic;
import com.github.dozermapper.core.Mapper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 项目请款计划表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Service
public class ProjRequestFundsPlanLogic extends BaseLogic<ProjRequestFundsPlan> {

    @Autowired
    private Mapper mapper;

    @Autowired
    ProjRequestFundsPlanDao projRequestFundsPlanDao;

    @Autowired
    ProjFilesLogic projFilesLogic;

    @Autowired
    ProjPaymentLogic projPaymentLogic;

    @Override
    public BaseEntityDao<ProjRequestFundsPlan, Integer> getDao() {
        return projRequestFundsPlanDao;
    }

    /**
     * 获取列表
     *
     * @param query
     * @return
     */
    public List<ProjRequestFundsPlanVO> list(ProjRequestFundsPlanQuery query) {
        List<ProjRequestFundsPlanVO> list = projRequestFundsPlanDao.list(query);
        list.stream().forEach(i -> {
            List<ProjFilesVO> files = projFilesLogic.listByProjIdAndStageId(i.getProjId(), i.getReqFundsId());
            i.setFiles(files);
        });
        return list;
    }

    /**
     * 获取分页列表
     *
     * @param query
     * @return
     */
    public DataPager<ProjRequestFundsPlanVO> listPage(ProjRequestFundsPlanQuery query) {
        return projRequestFundsPlanDao.listPage(query);
    }

    /**
     * 新增/修改
     *
     * @param userId
     * @param dto
     */
    public void insertOrUpdate(String userId, ProjRequestFundsPlanFilesDTO dto) {
        dto.getPlanList().stream().forEach(plan -> {
            if (ObjectUtil.isEmpty(plan.getId())) {
                plan.setReqFundsId(UUID.randomUUID().toString());
                insert(userId, plan);

                if (ObjectUtil.isNotEmpty(plan.getFiles())) {
                    plan.getFiles().stream().forEach(i -> {
                        i.setProjId(plan.getProjId());
                        i.setProjStageId(plan.getReqFundsId());
                    });
                    projFilesLogic.insert(userId, plan.getFiles());
                }
            } else {
                update(userId, plan);

                if (ObjectUtil.isNotEmpty(plan.getFiles())) {
                    List<ProjFiles> files = plan.getFiles().stream().filter(i -> ObjectUtil.isEmpty(i.getId())).collect(Collectors.toList());
                    files.stream().forEach(i -> {
                        i.setProjId(plan.getProjId());
                        i.setProjStageId(plan.getReqFundsId());
                    });
                    projFilesLogic.insert(userId, files);
                }
            }
        });

        if (ObjectUtil.isNotEmpty(dto.getDeleteIds())) {
            delete(userId, dto.getDeleteIds());
        }
    }

    /**
     * 根据自增ID获得实体VO
     *
     * @param id
     * @return
     */
    public ProjRequestFundsPlanVO getVOById(Integer id) {
        ProjRequestFundsPlan projRequestFundsPlan = getEntity(id);
        ProjRequestFundsPlanVO vo = mapper.map(projRequestFundsPlan, ProjRequestFundsPlanVO.class);

        //  获得附件
        List<ProjFilesVO> files = projFilesLogic.listByProjIdAndStageId(vo.getProjId(), vo.getReqFundsId());

        vo.setFiles(files);
        return vo;
    }

    public ProjRequestFundsPlan getEntityByGuidId(String reqFundsId) {
        return this.projRequestFundsPlanDao.getEntityByGuidId(reqFundsId);
    }

    public boolean updatePaymentMoneyByReqFundsId(String projId, String reqFundsId) {
        if (ObjectUtil.isEmpty(reqFundsId)) {
            throw new RuntimeException("请款ID不能为空！");
        }
        ProjMoneyDTO moneyDTO = new ProjMoneyDTO();
        moneyDTO.setProjId(projId);
        moneyDTO.setReqFundsId(reqFundsId);
        // 根据请款ID查询支付记录
        ProjPaymentMoneyVO moneyVO = projPaymentLogic.getPaymentMoney(moneyDTO);
        // 修改请款金额
        return this.projRequestFundsPlanDao.updatePaymentMoneyByReqFundsId(reqFundsId, moneyVO.getPaymentMoney());
    }
}
